

#import "PUEditNicknameView.h"
#import "IQKeyboardManager.h"
#import "IQKeyboardReturnKeyHandler.h"
@implementation PUEditNicknameView

-(instancetype)initWithFoodEditNicknameView:(NSString *)nickname
{
    self = [super init];
    if (self) {
        self.sayHelloStr=@"";
        self.menuArray=[NSMutableArray arrayWithObjects:@"1人",@"3人",@"5人",@"10人", nil];
        self.menuStr=self.menuArray[0];
        self.nickname=nickname;
        [self _setupUI];
    }
    return self;
}

-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
}

- (void)_setupUI
{
    
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5/1.0];

    //backgroundView
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor=[UIColor whiteColor];
    bgView.center = self.center;
    bgView.center=CGPointMake(self.frame.size.width/2, self.frame.size.height*2/5);
    bgView.bounds = CGRectMake(0, 0, self.frame.size.width*0.786, self.frame.size.width*0.786*0.644);
    
//    bgView.bounds = CGRectMake(37, 300, self.frame.size.width*0.786, self.frame.size.width*0.786*0.644);
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 20;
    [self addSubview:bgView];
    self.bgView=bgView;
    
    UILabel *lab = [[UILabel alloc] init];
   
    lab.frame = CGRectMake(0,bgView.frame.size.height*0.082,bgView.frame.size.width,bgView.frame.size.height*0.117);
    lab.text=@"修改昵称";
   
    lab.textAlignment=NSTextAlignmentCenter;
    lab.font=[UIFont boldSystemFontOfSize:16];
    [bgView addSubview:lab];
    
    UIView *view=[[UIView alloc]init];
    view.frame = CGRectMake(bgView.frame.size.width*0.071,bgView.frame.size.height*0.321,bgView.frame.size.width*0.874,bgView.frame.size.width*0.874*0.189);
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = view.frame.size.height/2;
    view.layer.borderWidth = 1.0f;
    view.layer.borderColor = [[UIColor colorWithHex:0xE9EBED] CGColor];
    view.backgroundColor=[UIColor colorWithHex:0xF9F9F9];
    [bgView addSubview:view];
    
    self.textField = [[UITextField alloc]init];
//    self.textField.backgroundColor = [UIColor colorWithHex:0xF5F6FA];
    self.textField.font = [UIFont systemFontOfSize:16.0f];
    self.textField.textAlignment = NSTextAlignmentLeft;
    self.textField.frame=CGRectMake(20, 0, view.frame.size.width-40, view.frame.size.height);
    self.textField.textColor=[UIColor blackColor];
    self.textField.text=self.nickname;
    self.textField.placeholder=@"输入昵称";
    
//    [_textField setValue:[UIColor colorWithHex:0xFE1C52] forKeyPath:@"placeholderLabel.textColor"];
    [view addSubview:self.textField];
    //295
    //196
    [self.textField becomeFirstResponder];
    //左边按钮
    UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [updateButton setBackgroundColor:[UIColor whiteColor]];
    updateButton.frame = CGRectMake(bgView.frame.size.width*0.064, bgView.frame.size.height*0.673, bgView.frame.size.width*0.42, bgView.frame.size.width*0.42*0.354);
    updateButton.clipsToBounds = YES;
    updateButton.layer.cornerRadius =updateButton.frame.size.height/2;
    updateButton.layer.borderWidth = 1.0f;
    updateButton.layer.borderColor = [[UIColor colorWithHex:0xFF729A] CGColor];
    [updateButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [updateButton setTitle:@"取消" forState:UIControlStateNormal];
     updateButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [updateButton setTitleColor:[UIColor colorWithHex:0xFF729A] forState:UIControlStateNormal];
    updateButton.tag=1000;
    [bgView addSubview:updateButton];
//    
//    //右边按钮
    UIButton *rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rechargeBtn setBackgroundColor:[UIColor colorWithHex:0xFF729A]];
    rechargeBtn.frame = CGRectMake(bgView.frame.size.width*0.535,  bgView.frame.size.height*0.673,bgView.frame.size.width*0.42, bgView.frame.size.width*0.42*0.354);
    rechargeBtn.clipsToBounds = YES;
    rechargeBtn.layer.cornerRadius =rechargeBtn.frame.size.height/2;
    [rechargeBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [rechargeBtn setTitle:@"确定" forState:UIControlStateNormal];
     rechargeBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [rechargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rechargeBtn.tag=1001;
    [bgView addSubview:rechargeBtn];
    
    
    [self showWithAlert:bgView];//添加动画的View
    
    //禁用IQKeyboardManager
    IQKeyboardManager *keyboardManager =  [IQKeyboardManager sharedManager];
    keyboardManager.enable = YES;
    keyboardManager.enableAutoToolbar = YES;
    
}
-(void)btnClicked:(UIButton*)button{
    if (button.tag==1001) {
        if ([[self.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length] == 0) {
          
            [self showSVProgressHUDStr:@"昵称不能为空"];
            
            return;
        }
    }
    [self dismissAlert];
    if (button.tag==1001) {
    
            if ([self.delegate respondsToSelector:@selector(CPInvitationCodeViewSureCode:)]) {
                [self.delegate CPInvitationCodeViewSureCode:self.textField.text];
            }
        
        
    }
}

-(void)showSVProgressHUDStr:(NSString *)str{
    if ([str isKindOfClass:[NSNull class]]||str==nil||[str isEqualToString:@""]) {
        return;
    }
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:str];
}

/**
 添加Alert入场动画
 @param alert 添加动画的View
 */
- (void)showWithAlert:(UIView*)alert{
    
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.2f;
    
    NSMutableArray *values = [NSMutableArray array];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    [alert.layer addAnimation:animation forKey:nil];
}

/** 添加Alert出场动画 */
- (void)dismissAlert{
    [self.textView resignFirstResponder];
//    [UIView animateWithDuration:0.3 animations:^{
//        self.transform = (CGAffineTransformMakeScale(1.6, 1.6));
//        self.backgroundColor = [UIColor clearColor];
//        self.alpha = 0;
//    }completion:^(BOOL finished) {
    [self removeFromSuperview];
        
//    } ];

}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.frame = CGRectMake(self.frame.size.width/2-self.frame.size.width*0.893/2, 0, self.frame.size.width*0.893, self.frame.size.width*0.893*1.358);
        self.cancelButton.frame = CGRectMake(self.frame.size.width/2-15,self.bgView.frame.origin.y+self.bgView.frame.size.height+40,30,30);
    }];
    
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.center = self.center;
        self.cancelButton.frame = CGRectMake(self.frame.size.width/2-15,self.bgView.frame.origin.y+self.bgView.frame.size.height+40,30,30);
    }];
}

@end
